package com.yilin.tms.order.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.gson.reflect.TypeToken;
import com.yilin.commons.util.ArrayUtil;
import com.yilin.commons.util.JsonUtil;
import com.yilin.tms.core.application.PageData;
import com.yilin.tms.core.application.ReturnData;
import com.yilin.tms.core.commons.annotation.SkipLoginVerify;
import com.yilin.tms.core.commons.base.BaseController;
import com.yilin.tms.core.entity.data.contracted.ContractBody;
import com.yilin.tms.core.entity.data.document.Document;
import com.yilin.tms.core.entity.data.document.DocumentResource;
import com.yilin.tms.core.entity.data.monitor.TmsRootData;
import com.yilin.tms.core.entity.data.receipt.ReceiptOrder;
import com.yilin.tms.core.entity.order.clearing.ClearingDetail;
import com.yilin.tms.core.entity.order.clearing.GatherRecord;
import com.yilin.tms.core.entity.order.dispatch.DispatchOrder;
import com.yilin.tms.core.entity.order.dispatch.DispatchOrderNote;
import com.yilin.tms.core.entity.order.dispatch.DispatchOrderNoteDetail;
import com.yilin.tms.core.entity.order.goods.GoodsOrder;
import com.yilin.tms.core.entity.order.goods.GoodsOrderNote;
import com.yilin.tms.core.entity.order.goods.GoodsOrderNoteDetail;
import com.yilin.tms.core.entity.order.party.OrderAppraise;
import com.yilin.tms.core.entity.order.party.OrderAutLog;
import com.yilin.tms.core.entity.order.payment.OrderPaymentCost;
import com.yilin.tms.core.entity.order.payment.OrderSettlementCost;
import com.yilin.tms.core.entity.order.rob.RobOrder;
import com.yilin.tms.core.entity.order.rob.RobOrderNote;
import com.yilin.tms.core.entity.order.rob.RobOrderNoteDetail;
import com.yilin.tms.core.entity.order.transit.TransitOrder;
import com.yilin.tms.core.entity.order.transit.TransitOrderNote;
import com.yilin.tms.core.entity.order.transit.TransitOrderNoteDetail;
import com.yilin.tms.core.entity.order.transit.TransitOrderVehicleRecord;
import com.yilin.tms.order.mapper.clearing.ClearingDetailMapper;
import com.yilin.tms.order.mapper.clearing.GatherRecordMapper;
import com.yilin.tms.order.mapper.dispatch.DispatchOrderMapper;
import com.yilin.tms.order.mapper.dispatch.DispatchOrderNoteDetailMapper;
import com.yilin.tms.order.mapper.dispatch.DispatchOrderNoteMapper;
import com.yilin.tms.order.mapper.goods.GoodsOrderMapper;
import com.yilin.tms.order.mapper.goods.GoodsOrderNoteDetailMapper;
import com.yilin.tms.order.mapper.goods.GoodsOrderNoteMapper;
import com.yilin.tms.order.mapper.party.OrderAppraiseMapper;
import com.yilin.tms.order.mapper.party.OrderAutLogMapper;
import com.yilin.tms.order.mapper.payment.OrderPaymentCostMapper;
import com.yilin.tms.order.mapper.payment.OrderSettlementCostMapper;
import com.yilin.tms.order.mapper.rob.RobOrderMapper;
import com.yilin.tms.order.mapper.rob.RobOrderNoteDetailMapper;
import com.yilin.tms.order.mapper.rob.RobOrderNoteMapper;
import com.yilin.tms.order.mapper.transit.TransitOrderMapper;
import com.yilin.tms.order.mapper.transit.TransitOrderNoteDetailMapper;
import com.yilin.tms.order.mapper.transit.TransitOrderNoteMapper;
import com.yilin.tms.order.mapper.transit.TransitOrderVehicleRecordMapper;
import com.yilin.tms.order.service.payment.IOrderPaymentCostService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/order/migration/")
public class MigrationController extends BaseController {
    @Resource
    DispatchOrderMapper dispatchOrderMapper;
    @Resource
    DispatchOrderNoteMapper dispatchOrderNoteMapper;
    @Resource
    DispatchOrderNoteDetailMapper dispatchOrderNoteDetailMapper;

    @Resource
    GoodsOrderMapper goodsOrderMapper;
    @Resource
    GoodsOrderNoteMapper goodsOrderNoteMapper;
    @Resource
    GoodsOrderNoteDetailMapper goodsOrderNoteDetailMapper;

    @Resource
    RobOrderMapper robOrderMapper;
    @Resource
    RobOrderNoteMapper robOrderNoteMapper;
    @Resource
    RobOrderNoteDetailMapper robOrderNoteDetailMapper;

    @Resource
    TransitOrderMapper transitOrderMapper;
    @Resource
    TransitOrderNoteMapper transitOrderNoteMapper;
    @Resource
    TransitOrderNoteDetailMapper transitOrderNoteDetailMapper;

    @Resource
    GatherRecordMapper gatherRecordMapper;
    @Resource
    ClearingDetailMapper clearingDetailMapper;

    @Resource
    OrderSettlementCostMapper orderSettlementCostMapper;
    @Resource
    OrderPaymentCostMapper orderPaymentCostMapper;
    @Resource
    IOrderPaymentCostService orderPaymentCostService;

    @Resource
    OrderAutLogMapper orderAutLogMapper;
    @Resource
    OrderAppraiseMapper orderAppraiseMapper;
    @Resource
    TransitOrderVehicleRecordMapper transitOrderVehicleRecordMapper;




    /**
     * 派单移植
     * */
    @PostMapping("dispatchOrderList")
    @SkipLoginVerify
    public ReturnData<Object> dispatchOrderList(int page) {
        PageData<DispatchOrder> data = dispatchOrderMapper.selectPage(new PageData<>(page, 5000),new QueryWrapper<DispatchOrder>().orderByAsc("create_time"));
        return jsonView(SUCCESS_CODE, "移植完成",data.getList());
    }
    @PostMapping("dispatchOrderNoteList")
    @SkipLoginVerify
    public ReturnData<Object> dispatchOrderNoteList(int page) {
        PageData<DispatchOrderNote> data = dispatchOrderNoteMapper.selectPage(new PageData<>(page, 5000),new QueryWrapper<DispatchOrderNote>().orderByAsc("create_time"));
        return jsonView(SUCCESS_CODE, "移植完成",data.getList());
    }
    @PostMapping("dispatchOrderNoteDetailList")
    @SkipLoginVerify
    public ReturnData<Object> dispatchOrderNoteDetailList(int page) {
        PageData<DispatchOrderNoteDetail> data = dispatchOrderNoteDetailMapper.selectPage(new PageData<>(page, 5000),new QueryWrapper<DispatchOrderNoteDetail>().orderByAsc("create_time"));
        return jsonView(SUCCESS_CODE, "移植完成",data.getList());
    }

    /**
     * 货源移植
     * */
    @PostMapping("goodsOrderList")
    @SkipLoginVerify
    public ReturnData<Object> goodsOrderList(int page) {
        PageData<GoodsOrder> data = goodsOrderMapper.selectPage(new PageData<>(page, 5000),new QueryWrapper<GoodsOrder>().orderByAsc("create_time"));
        return jsonView(SUCCESS_CODE, "移植完成",data.getList());
    }
    @PostMapping("goodsOrderNoteList")
    @SkipLoginVerify
    public ReturnData<Object> goodsOrderNoteList(int page) {
        PageData<GoodsOrderNote> data = goodsOrderNoteMapper.selectPage(new PageData<>(page, 5000),new QueryWrapper<GoodsOrderNote>().orderByAsc("create_time"));
        return jsonView(SUCCESS_CODE, "移植完成",data.getList());
    }
    @PostMapping("goodsOrderNoteDetailList")
    @SkipLoginVerify
    public ReturnData<Object> goodsOrderNoteDetailList(int page) {
        PageData<GoodsOrderNoteDetail> data = goodsOrderNoteDetailMapper.selectPage(new PageData<>(page, 5000),new QueryWrapper<GoodsOrderNoteDetail>().orderByAsc("create_time"));
        return jsonView(SUCCESS_CODE, "移植完成",data.getList());
    }

    /**
     * 抢单移植
     * */
    @PostMapping("robOrderList")
    @SkipLoginVerify
    public ReturnData<Object> robOrderList(int page) {
        PageData<RobOrder> data = robOrderMapper.selectPage(new PageData<>(page, 5000),new QueryWrapper<RobOrder>().orderByAsc("create_time"));
        return jsonView(SUCCESS_CODE, "移植完成",data.getList());
    }
    @PostMapping("robOrderNoteList")
    @SkipLoginVerify
    public ReturnData<Object>robOrderNoteList(int page) {
        PageData<RobOrderNote> data = robOrderNoteMapper.selectPage(new PageData<>(page, 5000),new QueryWrapper<RobOrderNote>().orderByAsc("create_time"));
        return jsonView(SUCCESS_CODE, "移植完成",data.getList());
    }
    @PostMapping("robOrderNoteDetailList")
    @SkipLoginVerify
    public ReturnData<Object> robOrderNoteDetailList(int page) {
        PageData<RobOrderNoteDetail> data = robOrderNoteDetailMapper.selectPage(new PageData<>(page, 5000), new QueryWrapper<RobOrderNoteDetail>().orderByAsc("create_time"));
        return jsonView(SUCCESS_CODE, "移植完成",data.getList());
    }

    /**
     * 运单
     * */
    @PostMapping("transitOrderList")
    @SkipLoginVerify
    public ReturnData<Object> transitOrderList(int page) {
        PageData<TransitOrder> data = transitOrderMapper.selectPage(new PageData<>(page, 5000), new QueryWrapper<TransitOrder>().orderByAsc("create_time"));
        String toJson = JsonUtil.objectToJson(data.getList());
        List<Map<String, Object>> dataList = JsonUtil.jsonToObject(toJson, new TypeToken<List<Map<String, Object>>>() {});
        for (Map<String, Object> map : dataList) {
            if(map.get("tmsOrderCreateState")==null) continue;
            if(map.get("tmsOrderCreateState").equals("error")){
                map.put("tmsOrderCreateState","wait");
            }
            if(map.get("tmsOrderCreateState").equals("creating")){
                map.put("tmsOrderCreateState","wait");
            }
            if(map.get("tmsOrderCreateState").equals("success")){
                map.put("tmsOrderCreateState","complete");
            }
        }
        return jsonView(SUCCESS_CODE, "移植完成",dataList);
    }
    @PostMapping("transitOrderNoteList")
    @SkipLoginVerify
    public ReturnData<Object> transitOrderNoteList(int page) {
        PageData<TransitOrderNote> data = transitOrderNoteMapper.selectPage(new PageData<>(page, 5000),new QueryWrapper<TransitOrderNote>().orderByAsc("create_time"));
        return jsonView(SUCCESS_CODE, "移植完成",data.getList());
    }
    @PostMapping("transitOrderNoteDetailList")
    @SkipLoginVerify
    public ReturnData<Object> transitOrderNoteDetailList(int page) {
        PageData<TransitOrderNoteDetail> data = transitOrderNoteDetailMapper.selectPage(new PageData<>(page, 5000),new QueryWrapper<TransitOrderNoteDetail>().orderByAsc("create_time"));
        return jsonView(SUCCESS_CODE, "移植完成",data.getList());
    }

    /**
     * 汇总
     * */
    @PostMapping("gatherRecordList")
    @SkipLoginVerify
    public ReturnData<Object> gatherRecordList(int page) {
        PageData<GatherRecord> data = gatherRecordMapper.selectPage(new PageData<>(page, 5000),new QueryWrapper<GatherRecord>().orderByAsc("create_time"));
        return jsonView(SUCCESS_CODE, "移植完成",data.getList());
    }

    /**
     * 清算
     * */
    @PostMapping("clearingDetailList")
    @SkipLoginVerify
    public ReturnData<Object> clearingDetailList(int page) {
        PageData<ClearingDetail> data = clearingDetailMapper.selectPage(new PageData<>(page, 5000),new QueryWrapper<ClearingDetail>().orderByAsc("create_time"));
        return jsonView(SUCCESS_CODE, "移植完成",data.getList());
    }

    /**
     * 支付单1
     * */
    @PostMapping("releaseOrderPaymentCostList")
    @SkipLoginVerify
    public ReturnData<Object> releaseOrderPaymentCostList(int page) {
        PageData<OrderPaymentCost> data = orderPaymentCostMapper.selectPage(new PageData<>(page, 5000),new QueryWrapper<OrderPaymentCost>().eq("pay_site", OrderPaymentCost.PaySite.release).orderByAsc("create_time"));
        return jsonView(SUCCESS_CODE, "移植完成",data.getList());
    }

    /**
     * 支付单2
     * */
    @PostMapping("robSuccessOrderPaymentCostList")
    @SkipLoginVerify
    public ReturnData<Object> robSuccessOrderPaymentCostList(int page) {
        PageData<OrderPaymentCost> data = orderPaymentCostMapper.selectPage(new PageData<>(page, 5000),new QueryWrapper<OrderPaymentCost>().eq("pay_site", OrderPaymentCost.PaySite.robSuccess).orderByAsc("create_time"));
        return jsonView(SUCCESS_CODE, "移植完成",data.getList());
    }

    /**
     * 支付单3
     * */
    @PostMapping("deliveryOrderPaymentCostList")
    @SkipLoginVerify
    public ReturnData<Object> deliveryOrderPaymentCostList(int page) {
        PageData<OrderPaymentCost> data = orderPaymentCostMapper.selectPage(new PageData<>(page, 5000),new QueryWrapper<OrderPaymentCost>().eq("pay_site", OrderPaymentCost.PaySite.delivery).orderByAsc("create_time"));
        return jsonView(SUCCESS_CODE, "移植完成",data.getList());
    }

    /**
     * 支付单4
     * */
    @PostMapping("receiptOrderPaymentCostList")
    @SkipLoginVerify
    public ReturnData<Object> receiptOrderPaymentCostList(int page) {
        PageData<OrderPaymentCost> data = orderPaymentCostMapper.selectPage(new PageData<>(page, 5000),new QueryWrapper<OrderPaymentCost>().eq("pay_site", OrderPaymentCost.PaySite.receipt).orderByAsc("create_time"));
        return jsonView(SUCCESS_CODE, "移植完成",data.getList());
    }


    /**
     * 支付单5
     * */
    @PostMapping("clearingOrderPaymentCostList")
    @SkipLoginVerify
    public ReturnData<Object> clearingOrderPaymentCostList(int page) {
        PageData<OrderPaymentCost> data = orderPaymentCostMapper.selectPage(new PageData<>(page, 5000),new QueryWrapper<OrderPaymentCost>().eq("pay_site", OrderPaymentCost.PaySite.clearing).orderByAsc("create_time"));
        return jsonView(SUCCESS_CODE, "移植完成",data.getList());
    }

    /**
     * 结算单
     * */
    @PostMapping("orderSettlementCostList")
    @SkipLoginVerify
    public ReturnData<Object> orderSettlementCostList(int page) {
        PageData<OrderSettlementCost> data = orderSettlementCostMapper.selectPage(new PageData<>(page, 5000),new QueryWrapper<OrderSettlementCost>().orderByAsc("create_time"));
        return jsonView(SUCCESS_CODE, "移植完成",data.getList());
    }

    /**
     * 订单日志
     * */
    @PostMapping("orderAutLogList")
    @SkipLoginVerify
    public ReturnData<Object> orderAutLogList(int page) {
        PageData<OrderAutLog> data = orderAutLogMapper.selectPage(new PageData<>(page, 5000),new QueryWrapper<OrderAutLog>().orderByAsc("create_time"));
        return jsonView(SUCCESS_CODE, "移植完成",data.getList());
    }

    /**
     * 订单评价
     * */
    @PostMapping("orderAppraiseList")
    @SkipLoginVerify
    public ReturnData<Object> orderAppraiseList(int page) {
        PageData<OrderAppraise> data = orderAppraiseMapper.selectPage(new PageData<>(page, 5000),new QueryWrapper<OrderAppraise>().orderByAsc("create_time"));
        return jsonView(SUCCESS_CODE, "移植完成",data.getList());
    }

    /**
     * 运输车辆记录
     * */
    @PostMapping("transitOrderVehicleRecordList")
    @SkipLoginVerify
    public ReturnData<Object> transitOrderVehicleRecordList(int page) {
        PageData<TransitOrderVehicleRecord> data = transitOrderVehicleRecordMapper.selectPage(new PageData<>(page, 5000),new QueryWrapper<TransitOrderVehicleRecord>().orderByAsc("create_time"));
        return jsonView(SUCCESS_CODE, "移植完成",data.getList());
    }
























}
